package com.itany.shop.service.impl;

import com.itany.shop.dao.ItemDao;
import com.itany.shop.dao.OrderDao;
import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.DataAccessException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.util.ParameterUtil;
import com.itany.shop.vo.Cart;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/19 10:26
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = (OrderDao) ObjectFactory.getObject("orderDao");
    private ItemDao itemDao = (ItemDao) ObjectFactory.getObject("itemDao");

    @Override
    public String createOrder(Cart cart, User user) throws UserNotLoginException, ServiceException {
        if(null == cart || null == user){
            throw new UserNotLoginException("请先登录");
        }
        // 由于保存明细时需要获取到当前保存的订单的主键值
        // 所以此处必须先保存订单
        // 将保存订单生成的主键赋值给明细
        // 再对明细进行保存

        // 创建需要保存的订单信息
        Order order = new Order();
        order.setPrice(cart.getPrice());
        order.setUser(user);
        // 生成订单号
        // 前面四位英文字母,后面使用毫秒数
        String s = "qwertyuioplkjhgfdsazxcvbnm";
        StringBuffer buffer = new StringBuffer();
        for(int i = 0; i < 4; i++){
            buffer.append(s.charAt(new Random().nextInt(s.length())));
        }
        String no = buffer.toString()+new Date().getTime();
        order.setNo(no);

        try {
            // 保存订单并返回对应的主键值
            Integer orderId = orderDao.insertReturnPrimaryKey(order);
            // 将生成的主键赋值给对应的属性
            order.setId(orderId);

            // 获取需要保存的明细信息
            List<Item> items = cart.getItems();
            for(Item item : items){
                // 购物车中的每一条明细均需要保存到数据库中
                // 每一条明细均属于当前生成的订单
                item.setOrder(order);
                // 每遍历一次,保存一条明细信息
                itemDao.insertItem(item);
            }

        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }


        return no;
    }

    @Override
    public List<Order> findMyOrders(User user) throws ServiceException, UserNotLoginException {
        if(null == user){
            throw new UserNotLoginException("请先登录");
        }
        try {
            List<Order> orders = orderDao.selectByUserId(user.getId());
            return orders;
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public Order findDetail(String id) throws RequestParameterErrorException, ServiceException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        try {
            Order order = orderDao.selectDetailById(Integer.parseInt(id));
            return order;
        } catch (DataAccessException e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
